package com.t.bricks.bussiness.db.dao.simpleuser.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.t.bricks.bussiness.db.dao.simpleuser.SimpleUserDao;
import com.t.bricks.bussiness.db.entity.simpleuser.SimpleUser;
import com.t.bricks.bussiness.db.mapper.simpleuser.SimpleUserMapper;
import com.t.bricks.model.Log;
import com.t.bricks.model.MsgEmity;
import com.t.bricks.model.Page;

@Repository
public class SimpleUserDaoImpl implements SimpleUserDao {
	
	@Autowired
	private SimpleUserMapper simpleUserMapper;
	
	/**
	 * 添加
	 * @param simpleUser
	 * @return
	 */
	@Override
	public MsgEmity add(SimpleUser simpleUser) {
		try {
			int i = simpleUserMapper.add(simpleUser);
			if (i < 1) {
				Log.error("新增未能得到预期影响值(预期1):", i);
				return new MsgEmity(false, "新增失败!", 91);
			}
		} catch (Exception e) {
			Log.error("新增时发生异常:", e.getMessage());
			return new MsgEmity(false, "新增失败!", 92);
		}
		
		return new MsgEmity(true, "新增成功!");
	}
	
	/**
	 * 删除
	 * @param iId
	 * @return
	 */
	@Override
	public MsgEmity del(Integer iId) {
		try {
			int i = simpleUserMapper.del(iId);
			if (i < 1) {
				Log.error("删除未能得到预期影响值(预期1):", i);
				return new MsgEmity(false, "删除失败!", 91);
			}
		} catch (Exception e) {
			Log.error("删除时发生异常:", e.getMessage());
			return new MsgEmity(false, "删除失败!");
		}
		
		return new MsgEmity(true, "删除成功!");
	}
	
	/**
	 * 修改密码
	 * @param iId
	 * @param sOldPass
	 * @param sNewPass
	 * @param iVersion
	 * @param sModifieder
	 * @return
	 */
	@Override
	public MsgEmity editPass(Integer iId, String sOldPass, String sNewPass) {
		try {
			int i = simpleUserMapper.editPass(iId, sOldPass, sNewPass);
			if (i < 1) {
				Log.error("修改未能得到预期影响值(预期1):", i);
				return new MsgEmity(false, "修改失败!", 91);
			}
		} catch (Exception e) {
			Log.error("修改时发生异常:", e.getMessage());
			return new MsgEmity(false, "修改失败!");
		}
		
		SimpleUser simpleUser = null;
		try {
			simpleUser = simpleUserMapper.findById(iId);
		} catch (Exception e) {
			Log.error("查询时发生异常:", e.getMessage());
			return new MsgEmity(false, "查询失败!", 91);
		}
		
		if (null == simpleUser) {
			Log.debug("该用户不存在!");
			return new MsgEmity(false, "该用户不存在!", 92);
		}
		
		return new MsgEmity(true, "修改成功!", simpleUser);
	}
	
	/**
	 * 按名称查询用户信息
	 * @param sName
	 * @return
	 */
	@Override
	public MsgEmity findByName(String sName) {
		SimpleUser simpleUser = null;
		try {
			simpleUser = simpleUserMapper.findByName(sName);
		} catch (Exception e) {
			Log.error("查询时发生异常:", e.getMessage());
			return new MsgEmity(false, "查询失败!", 91);
		}
		
		if (null == simpleUser) {
			Log.debug("该用户不存在!");
			return new MsgEmity(false, "该用户不存在!", 92);
		}
		
		return new MsgEmity(true, "查询成功!", simpleUser);
	}
	
	/**
	 * 按iId查询用户信息
	 * @param iId
	 * @return
	 */
	@Override
	public MsgEmity findById(Integer iId) {
		SimpleUser simpleUser = null;
		try {
			simpleUser = simpleUserMapper.findById(iId);
		} catch (Exception e) {
			Log.error("查询时发生异常:", e.getMessage());
			return new MsgEmity(false, "查询失败!", 91);
		}
		
		if (null == simpleUser) {
			Log.debug("该用户不存在!");
			return new MsgEmity(false, "该用户不存在!", 92);
		}
		
		return new MsgEmity(true, "查询成功!", simpleUser);
	}
		
	/**
	 * 查询分页信息
	 * @param sName
	 * @param orders
	 * @param page
	 * @return
	 */
	@Override
	public MsgEmity findByPage(String sName, String orders, Page page) {
		int iCount = 0;
		int startRow = (page.getPageCurrent() -1) * page.getPageSize();
		List<SimpleUser> resultList = new ArrayList<SimpleUser>();
		page.setRecordList(resultList);

		try {
			iCount = simpleUserMapper.findByPageCount(sName);
		} catch (Exception e) {
			Log.error("查询数量发生异常", e.getMessage());
			return new MsgEmity(false, "查询失败！", page);
		}
		
		if (iCount < 1) {
			return new MsgEmity(true, "没有相关数据！", page);
		}
		
		page.setCountRow(iCount);
		
		List<SimpleUser> list = null;
		try {
			list = simpleUserMapper.findByPage(sName, orders, startRow, page.getPageSize());
			if (null == list) {
				Log.error("查询数据发生异常,执行的返回值为:null,预期值不应为null");
			}
		} catch (Exception e) {
			Log.error("查询分页数据发生异常:", e.getMessage());
		}

		if (list == null) {
			return new MsgEmity(false, "查询失败！", page);
		}

		page.setRecordList(list);
		return new MsgEmity(true, "查询成功！", page);
	}
	
	/**
	 * 修改用户信息
	 * @param iId
	 * @param sName
	 * @param sNewPass
	 * @return
	 */
	@Override
	public MsgEmity edit(Integer iId, String sName, String sNewPass) {
		SimpleUser simpleUser = null;
		try {
			simpleUser = simpleUserMapper.findById(iId);
		} catch (Exception e) {
			Log.error("查询时发生异常:", e.getMessage());
			return new MsgEmity(false, "查询失败!", 91);
		}
		
		if (null == simpleUser) {
			Log.debug("该用户不存在!");
			return new MsgEmity(false, "该用户不存在!", 92);
		}
		
		simpleUser.setiId(iId);
		simpleUser.setsName(sName.trim());
		simpleUser.setsPass(sNewPass.trim());
		simpleUser.setdModifiedDate(new Date());
		
		try {
			int i = simpleUserMapper.edit(simpleUser);
			if (i < 1) {
				Log.error("修改未能得到预期影响值(预期1):", i);
				return new MsgEmity(false, "修改失败!", 91);
			}
		} catch (Exception e) {
			Log.error("修改时发生异常:", e.getMessage());
			return new MsgEmity(false, "修改失败!");
		}
		
		return new MsgEmity(true, "修改成功!", simpleUser);
	}
	
	/**
	 * 根据关键值取对象
	 * @param simpleUserData 存放查询条件的SimpleUser对象
	 * @return
	 */
	@Override
	public MsgEmity findByKey(SimpleUser simpleUserData) {
		SimpleUser simpleUser = null;
		try {
			simpleUser = simpleUserMapper.findByKey(simpleUserData);
			if (null == simpleUser) {
				return new MsgEmity(false, "数据不存在！", 91);
			}
		} catch (Exception e) {
			Log.error("查询发生异常:", e.getMessage());
			return new MsgEmity(false, "查询失败！", 93);
		}
		
		return new MsgEmity(true, "查询成功！", simpleUser);
	}
	
	/**
	 * 根据关键值查数量
	 * @param simpleUserData 存放查询条件的SimpleUser对象
	 * @return
	 */
	@Override
	public int findCountByKey(SimpleUser simpleUserData) {
		int iCount = 0;
		try {
			iCount = simpleUserMapper.findCountByKey(simpleUserData);
		} catch (Exception e) {
			Log.error("查询发生异常:", e.getMessage());
			return 0;
		}
		
		return iCount;
	}
	
	/**
	 * 检查待修改内容是否存在重复数据
	 * @param simpleUserData
	 * @return 重复返回true
	 */
	@Override
	public boolean checkRepeatByEdit(SimpleUser simpleUserData) {
		try {
			int i = simpleUserMapper.checkRepeatByEdit(simpleUserData);
			return i > 0;
		} catch (Exception e) {
			Log.error("检查待修改内容是否存在重复数据发生异常:", e.getMessage());
			return false;
		}
	}
	
	/**
	 * 检查待新增内容是否存在重复数据
	 * @param simpleUserData
	 * @return 重复返回true
	 */
	@Override
	public boolean checkRepeatByAdd(SimpleUser simpleUserData) {
		try {
			int i = simpleUserMapper.checkRepeatByAdd(simpleUserData);
			return i > 0;
		} catch (Exception e) {
			Log.error("检查待新增内容是否存在重复数据发生异常:", e.getMessage());
			return false;
		}
	}
	
}
